$(document).ready(function () {
    App.init();
    TableManageFixedHeader.init();
    FormWizardValidation.init();

    $('.datepicker-default').datepicker({
        language: 'zh-CN',//显示中文
        format: 'yyyy-mm-dd',//显示格式
        minView: "month",//设置只显示到月份
        initialDate: new Date(),//初始化当前日期
        autoclose: true,//选中自动关闭
        todayBtn: 'linked'//显示今日按钮
    });


    // wrapCompanyTable();
    // wrapPersonalTable();
});

//分页展示
var table1;
var table2;
function wrapCompanyTable() {

    table1 = $("#modeltable").DataTable({
        "bAutoWidth": false,
        "order": [[0, 'desc']],
        "pagingType": "simple_numbers",//设置分页控件的模式
        searching: false,//屏蔽datatales的查询框
        aLengthMenu: [10],//设置一页展示10条记录
        "bLengthChange": false,//屏蔽tables的一页展示多少条记录的下拉列表
        "oLanguage": {  //对表格国际化
            "sLengthMenu": "每页显示 _MENU_条",
            "sZeroRecords": "没有找到符合条件的数据",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "没有记录",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            }
        },
        "processing": true, //打开数据加载时的等待效果
        "serverSide": true,//打开后台分页
        "ajax": {
            "url": "/channel/queryTableData",
            "dataSrc": "aaData",
            "data": function (params) {
                // 公司
                params.type = 2;
                params.companyResisterName = $("#companyRegisterName").val();
                params.companyName = $("#companyName").val();
                params.status = $("#status").val();

            }
        },
        "columns": [
            {"data": "tid", "bSortable": false},
            {"data": "companyRegisterName"},
            {"data": "legalPerson.name"},
            {"data": "companyRegisterDate"},
            {
                "render": function(data, type, row, meta) {
                    return row.businessStartDate + ' - ' + row.businessEndDate;
                }
            },
            {"data": "contact.name"},
            {"data": "contact.telephone"},
            {"data": "masterUser.realName"},
            {"data": "creditTotal.acceptAmount"},
            {"data": "creditTotal.lastAmount"},
            {"data": "creditTotal.endDate"},
            {"data": "creditTotal.depositRate"},
            {"data": "creditTotal.depositLastAmount"},
            {"data": "status"}
        ],

        "columnDefs": [{
            "targets": 14,//是操作按钮目标列
            "data": null,
            "render": function (data, type) {
                var detail =  "<a href='javascript:void(0);' onclick='toDetail("+ data.tid +")' class='btn-link'>查看详情</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var edit =  "<a href='javascript:void(0);' onclick='toDetail("+ data + ")' class='btn-link'>编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var del =  "<a href='javascript:void(0);' onclick='toDetail("+ data +")' class='btn-link'>删除</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var create =  "<a href='javascript:void(0);' onclick='toDetail(data)' class='btn-link'>渠道审核</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var creditApply =  "<a href='javascript:void(0);' onclick='toDetail(data)' class='btn-link'>垫资授信申请</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var first =  "<a href='javascript:void(0);' onclick='toDetail(data)' class='btn-link'>授信初审</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var last =  "<a href='javascript:void(0);' onclick='toDetail(data)' class='btn-link'>授信终审</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var contract =  "<a href='javascript:void(0);' onclick='toDetail(data)' class='btn-link'>上传合同</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var review =  "<a href='javascript:void(0);' onclick='toDetail(data)' class='btn-link'>风控复核</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var completeDeposit =  "<a href='javascript:void(0);' onclick='toDetail(data)' class='btn-link'>补齐保证金</a>&nbsp;&nbsp;&nbsp;&nbsp;";
                var ensureDeposit =  "<a href='javascript:void(0);' onclick='toDetail(data)' class='btn-link'>保证金确认</a>&nbsp;&nbsp;&nbsp;&nbsp;";

                return detail + edit + del + create + creditApply + first + last + contract + review + completeDeposit + ensureDeposit;

            }
        }]
    });
}

function wrapPersonalTable() {

    table2 = $("#modeltable2").DataTable({
        "bAutoWidth": false,
        "order": [[0, 'desc']],
        "pagingType": "simple_numbers",//设置分页控件的模式
        searching: false,//屏蔽datatales的查询框
        aLengthMenu: [10],//设置一页展示10条记录
        "bLengthChange": false,//屏蔽tables的一页展示多少条记录的下拉列表
        "oLanguage": {  //对表格国际化
            "sLengthMenu": "每页显示 _MENU_条",
            "sZeroRecords": "没有找到符合条件的数据",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "没有记录",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            }
        },
        "processing": true, //打开数据加载时的等待效果
        "serverSide": true,//打开后台分页
        "ajax": {
            "url": "/channel/queryTableData",
            "dataSrc": "aaData",
            "data": function (params) {

                // 个人
                params.type = 1;

            }
        },
        "columns": [
            {"data": "tid", "bSortable": false},
            {"data": "contact.name"},
            {"data": "contact.idNo"},
            {"data": "contact.companyName"},
            {"data": "contact.telephone"},
            {"data": "masterUser.realName"},
            {"data": "status"}
        ],

        "columnDefs": [{
            "targets": 7,//是操作按钮目标列
            "data": null,
            "render": function (data, type, row) {
                return "<a href='javascript:void(0);' onclick='toDetail(this)' class='btn-link'>查看</a>&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }]
    });
}

//查询
function search() {
    table.ajax.reload();
}

//去往编辑页面
function toedit(id) {
    window.location.href = "/re/tocheck/" + id;
}

function toDetail(tid) {
    //var objectId = $(row).closest('tr').find('td').eq(0).text();
    console.log(tid);

    window.location.href= "/channel/" + tid;

    
}

//重置
function reset() {
    $("#registerNo").val("");
    $("#companyName").val("");
    $("#creditCode").val("");
    $("#updateTimeStart").val("");
    $("#updateTimeEnd").val("");
}


function showChannel(type) {
    if (type === 1) {
        // 个人
        channelType = 1;
        table2.ajax.reload();

        $("#myTab li:eq(0)").removeClass("active");
        $("#myTab li:eq(1)").addClass("active");
        $("#myTab li:eq(2)").removeClass("active");
        $("#tab1").removeClass("active in");
        $("#tab2").addClass(" active in");
        $("#tab3").removeClass(" active in");
        $("#tab1").hide();
        $("#tab2").show();
        $("#tab3").hide();



    } else if(type === 2) {
        // 公司
        channelType = 2;
        table1.ajax.reload();

        $("#myTab li:eq(0)").addClass("active");
        $("#myTab li:eq(1)").removeClass("active");
        $("#myTab li:eq(2)").removeClass("active");
        $("#tab1").addClass("active in");
        $("#tab2").removeClass(" active in");
        $("#tab3").removeClass(" active in");
        $("#tab1").show();
        $("#tab2").hide();
        $("#tab3").hide();
    } else {
        // 新建渠道
        $("#myTab li:eq(0)").removeClass("active");
        $("#myTab li:eq(1)").removeClass("active");
        $("#myTab li:eq(2)").addClass("active");
        $("#tab1").removeClass(" active in");
        $("#tab2").removeClass(" active in");
        $("#tab3").addClass(" active in");
        $("#tab1").hide();
        $("#tab2").hide();
        $("#tab3").show();
    }
}


function showSubChannel(type) {

    if (type === 1) {
        // 个人
        $("#mySubTab li:eq(0)").removeClass("active");
        $("#mySubTab li:eq(1)").addClass("active");
        $("#subTab1").removeClass(" active in");
        $("#subTab2").addClass(" active in");
        $("#subTab1").hide();
        $("#subTab2").show();

    } else {
        // 公司
        $("#mySubTab li:eq(0)").addClass("active");
        $("#mySubTab li:eq(1)").removeClass("active");
        $("#subTab1").addClass(" active in");
        $("#subTab2").removeClass(" active in");
        $("#subTab1").show();
        $("#subTab2").hide();
    }
}

